#include "../NetIO.h"
#include <iostream>
#include <list>
#include "TestHandler.h"

using namespace std;

static int send_seq_ = 0;
static int connection_ = 0;
#define TRACE_FUNC TRACE_LOG(__FUNCTION__)
class Task
{
public:
	Task( void );
public:
	~Task( void );
	virtual int get_result( );
};


Task::Task( void )
{
	TRACE_FUNC;
}

Task::~Task( void )
{
}

int Task::get_result( )
{
	TestHandler handler;
	
	while( 1 )
	{
		Operation_Result result;
		
		if ( NetIO::async_operation_result( result ) == -1 )
			break;

		if( result.optype_ == OPTYPE_READ )
		{
			handler.read_complete( result.handle_, result.errcode_, result.bytes_transferred_, result.data_block_, result.act_ );
		}
		else if( result.optype_ == OPTYPE_WRITE )
		{
			handler.write_complete( result.handle_, result.errcode_, result.bytes_transferred_, result.data_block_, result.act_ );
		}
		else if( result.optype_ == OPTYPE_ACCEPT )
		{
			handler.accept_complete( result.handle_,result.errcode_, result.act_ );
		}

		if( result.optype_ == OPTYPE_CONNECT )
		{
			handler.connect_complete( result.handle_,result.errcode_, result.act_ );
		}
	}
	return	0;
}


void sig_exit( int signo )
{

}

int main( )
{
	SockOps::net_init( );
	Singleton<Acceptor>::instance( )->open( 1000 );
	Thread_Group threads;
	Task task;

	Singleton<Acceptor>::instance( )->async_accept(  );//NetIO::async_connect( "127.0.0.1", 8899 );
	while( 1 )
	{
		task.get_result();
		Singleton<Reactor>::instance( )->run_once();
	}
	
	threads.run_thread( Singleton<Reactor>::instance( ), &Reactor::run, 1 );
	threads.run_thread( &task, &Task::get_result, 1 );
	
//	
	
	threads.join_all( );
	return 0;
}

